package com.oa.controller;

import com.oa.mapper.UserRoleMapper;
import com.oa.pojo.Result;
import com.oa.pojo.RoleMenu;
import com.oa.pojo.UserRole;
import com.oa.service.UserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户-角色中间表：在线接口")
@RestController
@RequestMapping("/ur")
public class UserRoleController {
    @Autowired
    private UserRoleService userRoleService;

    @CrossOrigin
    @ApiOperation(value="查询用户角色中间表(id,roleId，userId)",httpMethod = "GET",notes = "返回包含用户类的result类型json格式")
    @GetMapping("/selectAll")
    public Result selectAll(Integer id, Integer roleId, Integer userId){
        System.out.println("执行用户角色查询接口");

        UserRole userRole = new UserRole();
        userRole.setId(id);
        userRole.setRoleId(roleId);
        userRole.setUserId(userId);


        List<UserRole> userRoleList = userRoleService.selectUserRole(userRole);

        if (userRoleList!=null) {
            return Result.success(userRoleList);
        }else {
            return Result.success();
        }
    }

    @CrossOrigin
    @ApiOperation(value="根据用户角色中间表的id删除(id)",httpMethod = "GET",notes = "返回包含用户类的result类型json格式")
    @GetMapping("/deleteUserRole")
    public Result deleteUserRole(Integer id){
        System.out.println("执行用户角色删除接口");

        int flag= userRoleService.deleteUserRole(id);
        if (flag==1){
            return Result.success();
        }else {
            return Result.error("没有id为"+id+"的用户菜单中间表");

        }
    }
    @CrossOrigin
    @ApiOperation(value="用户角色中间表信息更改",httpMethod = "POST",notes = "返回的result类型json格式")
    @PostMapping("/updateUserRole")
    public Result updateUserRole(@RequestBody UserRole userRole){
        System.out.println("执行用户角色更改接口");

        int flag= userRoleService.updateUserRole(userRole);
        if (flag==1) {
            return Result.success();
        }else {
            return Result.error("添加心部门失败，有重复属性");
        }
    }

    @CrossOrigin
    @ApiOperation(value="添加用户角色中间表",httpMethod = "POST",notes = "返回的result类型json格式")
    @PostMapping("/insertUserRole")
    public Result insertUserRole(@RequestBody UserRole userRole){
        System.out.println("执行用户角色添加接口");

        int flag=userRoleService.insertUserRole(userRole);

        if (flag==1) {
            return Result.success();
        }else {
            return Result.error("添加心部门失败，有重复属性");
        }
    }
}
